package com.hmall.item.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;




    @GetMapping("/list")
    public PageDTO list(@RequestParam("page") Integer page,@RequestParam("size") Integer size){
        Page<Item> pageInfo=new Page<>(page,size);
        LambdaUpdateWrapper<Item> updateWrapper=new LambdaUpdateWrapper();
        updateWrapper.orderByDesc(Item::getUpdateTime);
        Page<Item> itemIPage = itemService.page(pageInfo,updateWrapper);
        PageDTO pageDTO=new PageDTO();
        pageDTO.setTotal(itemIPage.getTotal());
        pageDTO.setList(itemIPage.getRecords());
        return pageDTO;
    }

    @GetMapping("/{id}")
    public Item getById(@PathVariable Long id){
        return itemService.getById(id);
    }

    @PostMapping
    public void save(@RequestBody Item item){
        itemService.saveItem(item);
    }
    @PutMapping("/status/{id}/{status}")
    public void updateStatus(@PathVariable Long id,@PathVariable Integer status){
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("status",status);
        updateWrapper.set("update_time",new Date());
        itemService.update(updateWrapper);
        Item item = itemService.getById(id);
        rabbitTemplate.convertAndSend("item.direct","item.update", JSON.toJSONString(item));
    }
    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable Long id){
        itemMapper.deleteById(id);
        rabbitTemplate.convertAndSend("item.direct","item.delete",id.toString());

    }

    @PutMapping
    public void update(@RequestBody Item item){
        item.setUpdateTime(new Date());
        itemService.updateById(item);
        rabbitTemplate.convertAndSend("item.direct","item.update", JSON.toJSONString(item));
    }

    @PutMapping("/stock/{itemId}/{num}")
    public void updateStock(@PathVariable Long itemId,@PathVariable Integer num){
        LambdaUpdateWrapper<Item> updateWrapper=new LambdaUpdateWrapper();
        updateWrapper.eq(Item::getId,itemId);
        updateWrapper.setSql("stock=stock-"+num);
        itemService.update(updateWrapper);
    }




}
